from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QRectF

class CircularProgressBar(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.value = 0  # 当前进度值
        self.max_value = 100  # 最大进度值
        self.setFixedSize(100, 100)  # 设置固定大小

    def setValue(self, value):
        """设置进度值"""
        self.value = value
        self.update()  # 触发重绘

    def paintEvent(self, event):
        """重绘事件"""
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)  # 抗锯齿

        # 绘制背景圆环
        pen = QPen(QColor("#cccccc"), 8, Qt.SolidLine, Qt.RoundCap)
        painter.setPen(pen)
        painter.drawArc(10, 10, 80, 80, 0, 360 * 16)

        # 绘制进度圆环
        progress_color = QColor("#4CAF50")
        pen = QPen(progress_color, 8, Qt.SolidLine, Qt.RoundCap)
        painter.setPen(pen)
        angle = int((self.value / self.max_value) * 360 * 16)  # 计算角度
        painter.drawArc(10, 10, 80, 80, 90 * 16, -angle)  # 从顶部开始绘制

        # 绘制进度文本
        painter.setPen(Qt.black)
        painter.setFont(self.font())
        painter.drawText(QRectF(0, 0, 100, 100), Qt.AlignCenter, f"{self.value}%")